package com.mku.parkingmanagement_service.service.impl;

import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mku.parkingmanagement_service.dto.FeeUserDTO;
import com.mku.parkingmanagement_service.entity.Fee;
import com.mku.parkingmanagement_service.entity.User;
import com.mku.parkingmanagement_service.mapper.FeeMapper;
import com.mku.parkingmanagement_service.mapper.UserMapper;
import com.mku.parkingmanagement_service.service.FeeService;
import com.mku.parkingmanagement_service.vo.comm.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @className: FeeServiceImpl
 * @author: Maoxx
 * @date: 2024/11/8 23:12
 * @Version: 1.0
 * @description:
 */

@Service
public class FeeServiceImpl extends ServiceImpl<FeeMapper, Fee> implements FeeService {

    @Resource
    private FeeMapper feeMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public ResultVO<Object> userFee(FeeUserDTO feeUserDTO) {
        if (feeUserDTO == null || SaFoxUtil.isEmpty(feeUserDTO.getUsername()) || SaFoxUtil.isEmpty(feeUserDTO.getMoney())) {
            return ResultVO.fail("充值失败,请检查用户名或金额是否为空",null);
        }
        User one = userMapper.selectOne(new QueryWrapper<User>().eq("username", feeUserDTO.getUsername()));
        if (one != null) {
            double money = one.getMoney() == null ? 0 : one.getMoney();
            double newMoney = money + feeUserDTO.getMoney();
            one.setMoney(newMoney);
            userMapper.updateById(one);
            return new ResultVO<>().message("充值成功").flag(true);
        }
        return ResultVO.fail("充值失败,请检查用户名是否正确",null);

    }
}
